package com.gzx.spring.db.service.impl;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzx.risk.core.apis.result.RiskResult;
import com.gzx.spring.db.entity.RiskRuleApi;
import com.gzx.spring.db.mapper.RiskRuleApiMapper;
import com.gzx.spring.db.service.RiskRuleApiService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class RiskRuleApiServiceImpl extends ServiceImpl<RiskRuleApiMapper, RiskRuleApi> implements RiskRuleApiService {

	@Override
	public void log(String rid, RiskResult result) {
		RiskRuleApi api = new RiskRuleApi();
		api.setRid(rid);
		api.setGroupName(result.getGroupName());
		String dataName = result.getDataName();
		int start = dataName.indexOf("_");
		if(start<=0) {
			api.setApiGroup(dataName);
			api.setApiName(dataName);
		}else {
			api.setApiGroup(dataName.substring(0, start));
			api.setApiName(dataName.substring(start + 1));
		}
		
		api.setRuleOrder(result.getOrder());
		api.setApiUsed(result.getApiUsed()); 
		api.setDataContent(result.getDataContent());
		if (result.getRuleResult() == null || result.getRuleResult()) {
			api.setRuleResult(String.valueOf(true));
			api.setRejectRule("");
			api.setRejectMsg("");
		} else {
			api.setRuleResult(String.valueOf(false));
			api.setRejectRule(result.getRejectRule());
			api.setRejectRule(result.getRejectMsg());
		}
		this.save(api);
	}
}
